package shiyan3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(20000);
        byte[] buf = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        String ip = packet.getAddress().getHostAddress();
        int port = packet.getPort();
        String data = new String(packet.getData(), 0, packet.getLength());
        System.out.println(ip + ":" + port);
        System.out.println(data);

        String senddata ="我已经收到";
        byte[] sendbuf2 = senddata.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendbuf2, sendbuf2.length,packet.getAddress(), packet.getPort());
        socket.send(sendPacket);

        socket.close();
    }
}
